﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using Microsoft.Office.Interop.Excel;
using Excel_12 = Microsoft.Office.Interop.Excel;
namespace QuanLyBanHang_N3A
{
    public partial class FormTheoiDoiBanHang : Form
    {
        public FormTheoiDoiBanHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }
        string LayTenSanPham(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].TenSP;
        }
        string LayTenNV(string MaNV)
        {
            List<NhanVienDTO> ds = new List<NhanVienDTO>();
            NhanVienBUS bus = new NhanVienBUS();
            ds = bus.LayThongTinNhanVien(MaNV);
            return ds[0].TenNV.ToString();
        }

        

        private void FormTheoiDoiBanHang_Load(object sender, EventArgs e)
        {
            int stt = 1;
           
            List<CTPhieuBanDTO> dsct = new List<CTPhieuBanDTO>();
            CTPhieuBanBUS busct = new CTPhieuBanBUS();
            dsct = busct.TheoDoiBanHang();
            //string tennv = LayTenNV(ds[i].MaNV);
            for (int j = 0; j < dsct.Count; j++)
            {
                string tensp = LayTenSanPham(dsct[j].MaSP);
                GridViewTheoDoiBH.Rows.Add(stt, dsct[j].NgayBanCT, tensp, dsct[j].TongSLBan);
                stt++;
            }
        
        }

        private void btnThoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXuatFile_Click(object sender, EventArgs e)
        {
            try
            {
                ApplicationClass excel = new ApplicationClass();
                excel.Application.Workbooks.Add(true);

                Excel_12.Application oExcel_12 = null;                //Excel_12 Application
                Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
                Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
                Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
                Excel_12.Range oRange = null;                         // Cell or Range in worksheet
                Object oMissing = System.Reflection.Missing.Value;
                oExcel_12 = new Excel_12.Application();
                // Make Excel_12 visible to the user.
                oExcel_12.Visible = true;
                // Set the UserControl property so Excel_12 won't shut down.
                oExcel_12.UserControl = true;
                // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                // Add a workbook.
                oBook = oExcel_12.Workbooks.Add(oMissing);
                // Get worksheets collection
                oSheetsColl = oExcel_12.Worksheets;

                oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

                for (int j = 0; j < GridViewTheoDoiBH.Columns.Count; j++)
                {
                    oRange = (Excel_12.Range)oSheet.Cells[1, j + 1];
                    oRange.Value2 = GridViewTheoDoiBH.Columns[j].HeaderText;
                }

                for (int i = 0; i < GridViewTheoDoiBH.Rows.Count; i++)
                {
                    for (int j = 0; j < GridViewTheoDoiBH.Columns.Count; j++)
                    {

                        oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1];
                        oRange.Value2 = GridViewTheoDoiBH.Rows[i].Cells[j].Value;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
